//
//  HCNewsCenterDetailController.m
//  Aierle
//
//  Created by jiayibai-02por on 2017/5/25.
//  Copyright © 2017年 WillyZhao. All rights reserved.
//

#import "HCNewsCenterDetailController.h"
#import "NewsListCell.h"
#import "NewsCenterHeadView.h"

@interface HCNewsCenterDetailController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) UITableView *newsListTable;
@property (nonatomic, strong) NSArray *dataArray;
@property (nonatomic, copy) NSString *imgUrl;

@end

@implementation HCNewsCenterDetailController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.textTitle = @"新闻中心";
    self.view.backgroundColor = [UIColor whiteColor];
    self.automaticallyAdjustsScrollViewInsets = NO;
    [self setUpTableView];
    
    self.dataRequest.delegate = self;
    
    //获取新闻列表数据
    [self.dataRequest post_HomeNewsList];
}

#pragma mark -- 数据请求回调
- (void)dataRequesterFinish:(DataRequester*)requester
          requesterWithType:(DataRequestType)aType receiveDictionary:(NSDictionary*)dictionary {

    if (aType == HC_HOMENEWSLIST_TYPE) {
        
        NSLog(@"新闻列表数据:%@",dictionary);
        if (dictionary) {
            
            self.dataArray = [NSArray arrayWithArray:[dictionary objectForKey:@"news_list"]];
            [self.newsListTable reloadData];
            self.imgUrl = [dictionary objectForKey:@"img"];
        }
    }
}
- (void)dataRequesterFail:(DataRequester*)requester requesterWithType:(DataRequestType)aType receiveErrorDictionary:(NSDictionary*)dictionary {

    if (aType == HC_HOMENEWSLIST_TYPE) {
        
        NSLog(@"数据请求失败");
    }
}

- (void)setUpTableView {

    self.newsListTable = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, Screen_width, Screen_height - 64) style:UITableViewStyleGrouped];
    self.newsListTable.delegate = self;
    self.newsListTable.dataSource = self;
    [self.newsListTable registerNib:[UINib nibWithNibName:@"NewsListCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"listCell"];
    self.newsListTable.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:self.newsListTable];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return 1;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {

    NewsCenterHeadView *headView = [[NewsCenterHeadView alloc] initWithFrame:CGRectMake(0, 0, Screen_width, YBKAdaptiveH(200))];
    headView.imageUrl = self.imgUrl;
    if (self.dataArray.count != 0) {
        
        headView.topLabelName = [self.dataArray[0] objectForKey:@"title"];
    }
    headView.backgroundColor = YBKRGBCOLOR(241, 241, 241, 1);
    return headView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {

    return YBKAdaptiveH(200);
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *identifier = @"listCell";
    NewsListCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell) {
        cell = [[NewsListCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    if (self.dataArray.count != 0) {
        
        cell.dataDic = self.dataArray[indexPath.row];
    }
    if (indexPath.row >= 3) {
        
        cell.hotView.hidden = YES;
    }
    else {
    
        cell.hotView.hidden = NO;
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {

    return 50;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    NSString *url = [self.dataArray[indexPath.row] objectForKey:@"url"];
    [self pushChildrenViewController:@"HCNeswDetailViewController" parameterObject:url dataObject:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

@end
